import xml.etree.ElementTree as ET
import time
from PySide6.QtWidgets import QWidget, QVBoxLayout, QTableWidget, QTableWidgetItem, QHeaderView, QTabWidget
import numpy as np
import matplotlib
matplotlib.use('QtAgg')

#from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qtagg import (
    FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure


class tabWidgetDrawingShow(QWidget):

    def __init__(self, dataReader):
        super().__init__()
        self.drawlayout = QVBoxLayout(self)
        self.drawingshow(dataReader)

    def drawingshow(self, dataReader):
        # imageshow
        xml_canvas = FigureCanvas(Figure()) # Figure内部可以放figsize=(15, 9)
        self.drawlayout.addWidget(NavigationToolbar(xml_canvas, self)) #窗口内嵌工具 
        self.drawlayout.addWidget(xml_canvas)
        # xml_canvas.setWindowTitle("XML Data")               
        self._static_ax = xml_canvas.figure.subplots()
        for title, data in dataReader.seriesData:
            if data:
                index, x_values, y_values = zip(*data) 
                self._static_ax.plot(x_values, y_values, label=title)
    
    '''
        def XMLfiletableshow(self):
        #tableshow
        xml_table = QTableWidget()
        #xml_table.setHorizontalHeaderLabels(['title','data'])
        #xml_table.verticalHeader().setVisible(False)
        xml_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        xml_table.setRowCount(len(series_data))
        xml_table.setColumnCount(3)
        for i, (title, data) in enumerate(series_data):
            xml_table.setItem(i, 0, QTableWidgetItem(title))
            for j, (x, y) in enumerate(data):
                xml_table.setItem(i, j+1, QTableWidgetItem(str(x)))
                xml_table.setItem(i, j+2, QTableWidgetItem(str(y)))
        
        #for i in range(len(series_data)):
        #    xml_table.setItem(i,0,QTableWidgetItem(series_data[i][0]))
        #    xml_table.setItem(i,1,QTableWidgetItem(str(series_data[i][1])))
        

        self.tablelayout.addWidget(xml_table)
        # xml_table.setWindowTitle("XML Data")
        # xml_table.show()
        # self.setLayout(self.layout)


    '''


class tabWidgetTableShow(QWidget):

    def __init__(self, dataReader):
        super().__init__()
        self.layout = QVBoxLayout(self)
        self.tabWidget = QTabWidget()
        self.layout.addWidget(self.tabWidget)

        self.tableShow(dataReader)
        #if self.tablelayout:
            #del self.tablelayout
#        self.tablelayout = QVBoxLayout(self)

    def tableShow(self, dataReader):
        for (title , data) in dataReader.seriesData:
            subWidget = self.subTableShow(data)
            self.tabWidget.addTab(subWidget, title)
    

    def subTableShow(self, data):
        #tableshow
        xml_table = QTableWidget(self)
        xml_table.setColumnCount(3)
        xml_table.setHorizontalHeaderLabels(['Index','X', 'Y'])
        xml_table.verticalHeader().setVisible(False)
        xml_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        xml_table.setRowCount(len(data))
        for i, (index, x, y) in enumerate(data):
            xml_table.setItem(i, 0, QTableWidgetItem(str(index)))
            xml_table.setItem(i, 1, QTableWidgetItem(str(x)))
            xml_table.setItem(i, 2, QTableWidgetItem(str(y)))
        
        return xml_table
        #for i in range(len(series_data)):
        #    xml_table.setItem(i,0,QTableWidgetItem(series_data[i][0]))
        #    xml_table.setItem(i,1,QTableWidgetItem(str(series_data[i][1])))
        # self.tablelayout.addWidget(xml_table)
